/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.dao.BaseDAO;
import etcore.webapp.grouptalk.common.dao.CommentDAO;
import etcore.webapp.grouptalk.common.dao.GroupDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.dao.MessageDAO;
import etcore.webapp.grouptalk.common.dao.TopicDAO;
import etcore.webapp.grouptalk.common.object.Comment;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.object.Message;
import etcore.webapp.grouptalk.common.object.Topic;
import etcore.webapp.grouptalk.user.object.Member;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * An instance of MemberDAO is a data access object used to access Member, which
 * contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class MemberDAO extends BaseDAO {

	public static final String MEMBER = "member";
	public static final String ADMINISTRATOR = "administrator";
	public static final String OWNER = "owner";
	public static final String ROOTS = "roots";

	public long attendGroupNum(String u, String mytype) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.userName=? and model.mytype=? and model.locked=0");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, u);
		queryObject.setParameter(1, mytype);
		Object result = queryObject.uniqueResult();

		if (result != null)
			return ((Number) result).longValue();

		return 0;
	}

	public long energySum(String u) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select sum(energy) from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.userName=? ");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, u);
		Object result = queryObject.uniqueResult();
		if (result != null)
			return ((Number) result).longValue();

		return 0;
	}

	public List<Member> findAll() {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;

	}

	public List<Member> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;

	}

	public List<Member> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? order by id desc");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;

	}

	public Member findByUsernameAndGcid(String u, Integer commentid) {

		try {
			CommentDAO gcdao = new CommentDAO();
			Comment comment = gcdao.findById(commentid);
			if (comment != null)
				return findByUsernameAndGmid(u, comment.getGmid());
			else
				return null;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Member findByUsernameAndGid(String u, Integer groupid) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.userName=? and model.gid=?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, u);
		queryObject.setParameter(1, groupid);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();

		if (results != null && results.size() > 0)
			return (Member) results.get(0);
		else
			return null;

	}

	public Member findByUsernameAndGmid(String userName, Integer messageid) {

		try {
			MessageDAO gmdao = new MessageDAO();
			Message message = gmdao.findById(messageid);
			if (message != null)
				return findByUsernameAndTid(userName, message.getTid());
			else
				return null;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Member findByUsernameAndTid(String userName, Integer topicid) {

		try {
			TopicDAO dao = new TopicDAO();
			Topic topic = dao.findById(topicid);
			if (topic != null)
				return findByUsernameAndGid(userName, topic.getGid());
			else
				return null;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("rawtypes")
	public List findEnglishGroupStars(Integer open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  model.gid, gmodel.name, count(model.gid),gmodel.pic from gt_member as model, gt_group as gmodel where model.gid=gmodel.id and gmodel.open=? and gmodel.en=1  group by model.gid order by count(model.gid) desc";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findEnglishMemberGroups(Integer open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  model.userName, gmodel.id, gmodel.name from gt_member as model, gt_group as gmodel where model.gid=gmodel.id and gmodel.open=? and gmodel.en=1 and STRCMP(now(),model.createTime)>=0 order by model.id desc";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findGroupStars(Integer open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  model.gid, gmodel.name, count(model.gid) from gt_member as model, gt_group as gmodel where model.gid=gmodel.id and gmodel.open=? group by model.gid order by count(model.gid) desc";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findLocalGroupStars(Integer open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  model.gid, gmodel.name, count(model.gid),gmodel.pic from gt_member as model, gt_group as gmodel where model.gid=gmodel.id and gmodel.open=? and gmodel.local=1 group by model.gid order by count(model.gid) desc";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findLocalMemberGroups(Integer open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  model.userName, gmodel.id, gmodel.name,gmodel.pic from gt_member as model, gt_group as gmodel where model.gid=gmodel.id and gmodel.open=? and gmodel.local=1 and STRCMP(now(),model.createTime)>=0 order by model.id desc";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public List<Member> findMember(Integer gid, String type, Integer lo) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.gid=? and model.mytype=? and model.locked=?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, type);
		queryObject.setParameter(2, lo);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;
	}

	public List<Member> findMember(Integer gid, String type, Integer lock,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.gid=? and model.mytype=? and model.locked=?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, type);
		queryObject.setParameter(2, lock);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findMemberGroups(Integer open, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "";

		queryString = "select  model.userName, gmodel.id, gmodel.name from gt_member as model, gt_group as gmodel where model.gid=gmodel.id and gmodel.open=? order by model.id desc";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();

		return results;

	}

	public List<Member> getAllMember(Integer gid, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.gid=? and ( (model.mytype='member' and model.locked<>1) or model.mytype='administrator' or model.mytype='owner') ");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, gid);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;

	}
	public List<Member> getLockedMember(Integer gid, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.gid=? and   (model.mytype='member' and model.locked=1  ) ");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, gid);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Member> results = queryObject.list();
		return results;

	}
	public long getAllMemberNum(Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(id) from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.gid=? and ( (model.mytype='member' and model.locked<>1 ) or model.mytype='administrator' or model.mytype='owner')");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, gid);

		Object result = queryObject.uniqueResult();

		return result == null ? 0 : ((Number) result).longValue();

	}

	public long getMemberNum(Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(gt_user.id) as ct  from gt_user,gt_member where gt_user.userName=gt_member.userName and gt_member.gid=?  and gt_user.isLock<>-1 and STRCMP(now(),gt_user.regTime)>=0";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		sqlQuery.setParameter(0, gid);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return ((results == null||results.size()==0) ||results.size()==0)  ? 0 : ((Number) results.get(0)).longValue();

	}
	public long getLockedMemberNum(Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(gt_user.id) as ct  from gt_user,gt_member where gt_user.userName=gt_member.userName and gt_member.gid=? and gt_member.locked=1 and STRCMP(now(),gt_user.regTime)>=0";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);

		sqlQuery.setParameter(0, gid);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return ((results == null||results.size()==0) ||results.size()==0)  ? 0 : ((Number) results.get(0)).longValue();

	}

	public long getMemberNum(Integer gid, String type, Integer lock) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(id) from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.gid=? and model.mytype=? and model.locked=?");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, type);
		queryObject.setParameter(2, lock);
		Object result = queryObject.uniqueResult();

		return result == null ? 0 : ((Number) result).longValue();

	}

	public long getAllSysAdminNum() {
		GroupDAO groupDao = new GroupDAO();
		Group group = groupDao.findByName(ROOTS);
		if (group != null) {

			return getAllMemberNum(group.getId());
		}

		return 0;
	}

	public String getAllSystemAdmins(int begin, int count) {
		GroupDAO groupDao = new GroupDAO();
		Group group = groupDao.findByName(ROOTS);
		if (group != null) {
			StringBuffer stringBuffer = new StringBuffer();
			List<Member> members = getAllMember(group.getId(), begin, count);
			int num = 0;
			if (members != null) {
				for (Member member : members) {
					if (num > 0)
						stringBuffer.append(Symbols.COMMA);
					stringBuffer.append(member.getUserName());
					num++;
				}
				return stringBuffer.toString();
			}
		}
		return null;
	}

	public long getSysAdminNum() {
		GroupDAO groupDao = new GroupDAO();
		Group group = groupDao.findByName(ROOTS);
		if (group != null) {

			return getMemberNum(group.getId());
		}

		return 0;
	}

	@SuppressWarnings("unchecked")
	public String getSystemAdmins(int begin, int count) {
		GroupDAO groupDao = new GroupDAO();
		Group group = groupDao.findByName(ROOTS);

		if (group != null) {
			Session session = HibernateSessionFactory.getSession();
			String queryString = "select gt_member.userName from gt_member,gt_user where gt_user.userName=gt_member.userName and gt_member.gid=? and  gt_user.isLock<>-1 and STRCMP(now(),gt_user.regTime)>=0";

			Query queryObject = session.createSQLQuery(queryString);
			queryObject.setParameter(0, group.getId());
			queryObject.setFirstResult(begin);
			queryObject.setMaxResults(count);
			List<String> results = queryObject.list();

			int num = 0;
			if (results != null) {
				StringBuffer stringBuffer = new StringBuffer();
				for (String userName : results) {
					if (num > 0)
						stringBuffer.append(Symbols.COMMA);
					stringBuffer.append(userName);
					num++;
				}
				return stringBuffer.toString();
			}
		}
		return null;
	}
	public long findSizeOfAdminNotFollowing(Integer open,String fromUser) {

		Session session = HibernateSessionFactory.getSession();
		String queryString =  "SELECT COUNT(g2.name) as ct FROM gt_group g2 WHERE g2.open="+open+" AND g2.id NOT IN (SELECT m2.gid FROM gt_member m2 WHERE m2.userName=?)";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
	 
		sqlQuery.setParameter(0, fromUser);
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return ((results == null||results.size()==0) ||results.size()==0)  ? 0 : ((Number) results.get(0)).longValue();

	}
	public List<Integer>   findGroupsOfAdminNotFollowing(Integer open,String fromUser,int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "SELECT g2.id FROM gt_group g2 WHERE g2.open="+open+" AND g2.id NOT IN (SELECT m2.gid FROM gt_member m2 WHERE m2.userName=?)";

		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, fromUser);
 
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Integer> results =  queryObject.list();

		return results;

	}
	public long findSizeOfAdminFollowing(Integer open) {

		Session session = HibernateSessionFactory.getSession();
		String queryString =  "SELECT COUNT(m2.id) as ct FROM gt_member m1, gt_member m2,gt_group g1,gt_group g2 WHERE m2.userName=m1.userName AND m2.gid=g2.id AND g1.id=m1.gid AND g1.name='ROOTS' AND g2.open="+open;

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
	 
		 
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null||results.size()==0)  ? 0 : ((Number) results.get(0)).longValue();

	}
	public long findSizeOfAdminFollowing(Integer open,String inputDate) {

		Session session = HibernateSessionFactory.getSession();
		String queryString =  "SELECT COUNT(m2.id) as ct FROM gt_member m1, gt_member m2,gt_group g1,gt_group g2 WHERE m2.userName=m1.userName AND m2.gid=g2.id AND g1.id=m1.gid AND g1.name='ROOTS' AND (TO_DAYS("+inputDate+")-TO_DAYS(m2.createTime))=0 AND g2.open="+open;

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
	 
		 
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null||results.size()==0)  ? 0 : ((Number) results.get(0)).longValue();

	}
	public long findAdminsSizeByMaxHisFollowed(Integer open,long  maxFollowed ) {

		Session session = HibernateSessionFactory.getSession();
		String queryString =  "select count(*) as ct from (SELECT  m.userName,(SELECT COUNT(*) FROM gt_member m2,gt_group g2 WHERE m.userName=m2.userName AND g2.open=2 AND g2.id=m2.gid ) AS d   FROM gt_member m,gt_group g WHERE    m.gid=g.id AND g.name='ROOTS'  GROUP BY m.userName HAVING d<"+maxFollowed+")sample";

		SQLQuery sqlQuery = session.createSQLQuery(queryString).addScalar("ct",
				Hibernate.INTEGER);
	 
		 
		@SuppressWarnings("rawtypes")
		List results = sqlQuery.list();

		return (results == null||results.size()==0)  ? 0 : ((Number) results.get(0)).longValue();

	}
	
	public List<Object[]> findAdminsByMaxHisFollowed(Integer open,long  maxFollowed,int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "SELECT  m.userName,(SELECT COUNT(*) FROM gt_member m2,gt_group g2 WHERE m.userName=m2.userName AND g2.open=2 AND g2.id=m2.gid ) AS d   FROM gt_member m,gt_group g WHERE    m.gid=g.id AND g.name='ROOTS'  GROUP BY m.userName HAVING d<"+maxFollowed;

		 

		Query queryObject = session.createSQLQuery(queryString);

		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Object[]> results = queryObject.list();

		return results;

	}
	public boolean isAdmin(String userName, Integer gid) {
		if (userName == null || gid == null)
			return false;
		int isadmin = 0;
		Member gmem = findByUsernameAndGid(userName, gid);

		if (gmem != null
				&& ((gmem.getMytype().equals(ADMINISTRATOR) && gmem.getLocked()
						.longValue() == 0) || gmem.getMytype().equals(OWNER)))
			isadmin = 1;

		if (isadmin == 1)
			return true;
		return false;

	}

	public long isIn(String u, Integer groupid) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Member.class.getSimpleName());
		sqlStringBuffer
				.append(" as model where model.userName=? and model.gid=?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, u);
		queryObject.setParameter(1, groupid);
		@SuppressWarnings("rawtypes")
		List results = queryObject.list();
		if (results != null && results.size() > 0)
			return 1;
		return 0;
	}

	public boolean isMember(String userName, Integer gid) {
		if (userName == null || gid == null)
			return false;
		int isowner = 0, isadmin = 0, ismem = 0, tobeadmin = 0;
		Member gmem = findByUsernameAndGid(userName, gid);

		if (gmem != null && gmem.getMytype().equals(OWNER))
			isowner = 1;
		if (gmem != null
				&& ((gmem.getMytype().equals(ADMINISTRATOR) && gmem.getLocked()
						.longValue() == 0) || gmem.getMytype().equals(OWNER)))
			isadmin = 1;
		if (gmem != null && gmem.getMytype().equals(ADMINISTRATOR)
				&& gmem.getLocked().longValue() == 1)
			tobeadmin = 1;
		if (gmem != null
				&& ((gmem.getMytype().equals(MEMBER) && gmem.getLocked()
						.longValue() != 1) || isadmin == 1) || tobeadmin == 1
				|| isowner == 1)
			ismem = 1;
		if (ismem == 1)
			return true;
		return false;

	}

	public boolean isOwner(String userName, Integer gid) {
		if (userName == null || gid == null)
			return false;
		int isowner = 0;
		Member gmem = findByUsernameAndGid(userName, gid);

		if (gmem != null && gmem.getMytype().equals(OWNER))
			isowner = 1;

		if (isowner == 1)
			return true;
		return false;

	}

	public boolean isSysAdmin(String userName) {
		GroupDAO groupDao = new GroupDAO();
		Group group = groupDao.findByName(ROOTS);
		if (group != null) {
			Member member = findByUsernameAndGid(userName, group.getId());
			if (member != null)
				return true;
		}
		return false;
	}
}